﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class TransferDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILpnDA lpnDA = DataAccess.CreateLpnDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();
        private static readonly ITransferDA transferDA = DataAccess.CreateTransferDA();
        private static readonly ITransferDetailDA transferDetailDA = DataAccess.CreateTransferDetailDA();

        public TransferDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public TransferDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TransferDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteTransferDetail(List<string> transferIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (transferIDs.Count != LineIDs.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F5300_005", this.pubParasInfo.UserLanguage);
                }
                List<TransferDetailInfo> transferDetailInfos = new List<TransferDetailInfo>();
                TransferDetailInfo transferDetailInfo = null;
                for (i = 0; i < transferIDs.Count; i++)
                {
                    transferDetailInfo = transferDetailDA.GetTransferDetailByID(dataBase, tran, transferIDs[i], LineIDs[i], this.dataBaseID[0]);
                    transferDetailInfos.Add(transferDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (transferDetailDA.DeleteTransferDetail(dataBase, tran, transferIDs, LineIDs, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < transferIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TransferDetailInfo>(dataBase, tran, transferDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + transferIDs[i] + " , " + LineIDs[i] + " ; ";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public TransferDetailInfo GetTransferDetailInfoByID(string transferID, string LineID)
        {
            return transferDetailDA.GetTransferDetailByID(transferID, LineID, this.dataBaseID[0]);
        }

        public string InsertTransferDetailInfo(TransferDetailInfo transferDetailInfo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string transferLine = string.Empty;
            try
            {
                SysSettingInfo sysSetting = sysSettingDA.GetSysSettingByID(dataBase, tran, "LPNWhetherToallowDuplicate", whLoginID, this.pubParasInfo);
                if (((sysSetting != null) && (sysSetting.Value == "N")) && lpnDA.CheckLpnIDUnique(dataBase, tran, transferDetailInfo.ToLpnID, whLoginID))
                {
                    throw new Exception("LPN已经存在,请重新输入!");
                }
                transferDetailInfo.UpdatedBy = transferDetailInfo.CreatedBy;
                transferDetailInfo.CreatedDate = DateTime.Now;
                transferDetailInfo.UpdatedDate = DateTime.Now;
                string lotID = new InboundHelper().CheckLotAttributeInfo(dataBase, tran, transferDetailInfo.TransferLotInfo, transferDetailInfo.ToOwnerID, transferDetailInfo.ToSkuID, whLoginID, transferDetailInfo.UpdatedBy);
                transferDetailInfo.ToLot = lotID;
                if (transferDetailDA.InsertTransferDetail(dataBase, tran, transferDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TransferDetailInfo>(null, transferDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                    transferLine = transferDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return transferLine;
        }

        public string UpdateTransferDatail(TransferDetailInfo transferDetailInfo)
        {
            string LineID = string.Empty;
            try
            {
                SysSettingInfo sysSetting = sysSettingDA.GetSysSettingByID("LPNWhetherToallowDuplicate", this.dataBaseID[0], this.pubParasInfo);
                if (((sysSetting != null) && (sysSetting.Value == "N")) && lpnDA.CheckLpnIDUnique(transferDetailInfo.ToLpnID, this.dataBaseID[0]))
                {
                    throw new Exception("LPN已经存在,请重新输入!");
                }
                TransferDetailInfo oldTransferDetailInfo = transferDetailDA.GetTransferDetailByID(transferDetailInfo.TransferID, transferDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                string lotID = new InboundHelper().CheckLotAttributeInfo(transferDetailInfo.TransferLotInfo, transferDetailInfo.ToOwnerID, transferDetailInfo.ToSkuID, this.dataBaseID[0], this.pubParasInfo.UserID);
                transferDetailInfo.ToLot = lotID;
                if (transferDetailDA.UpdateTransferDetail(transferDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<TransferDetailInfo>(oldTransferDetailInfo, transferDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "TRANSFER_DETAIL", this.dataBaseID[0]);
                    LineID = transferDetailInfo.TransferID + " , " + transferDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }
    }
}

